<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>库存拆包编辑</title>
<link href="/resources/lib/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
<link href="/resources/lib/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
<script src="/resources/lib/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="/resources/lib/ligerUI/js/ligerui.all.js" type="text/javascript"></script>
<script src="/resources/lib/jquery-validation/jquery.validate.min.js" type="text/javascript"></script> 
<script src="/resources/lib/jquery-validation/jquery.metadata.js" type="text/javascript"></script>
<script src="/resources/lib/jquery-validation/messages_cn.js" type="text/javascript"></script>
<script src="/resources/lib/json2.js" type="text/javascript"></script>
<script src="/resources/js/jsF7.js" type="text/javascript"></script>
<script src="/resources/js/selectData.js" type="text/javascript"></script>
<script src="/resources/js/themis.js" type="text/javascript"></script>
<script src="/resources/js/validation.js" type="text/javascript"></script>

<script type="text/javascript">
	var form1;
	var form2;
	var grid;
	$(function(){
	    //工具条
	    $("#toptoolbar").ligerToolBar({ items: [
	        	{ text: '保存', id:'save',click: saveForm,icon: 'save' },
	        	{ text: '提交', id:'save',click: submitForm },
	        	{ text: '审核', id:'save',click: auditForm },
	        	{ text: '反审核', id:'save',click: unAuditForm},

	    	]
	    });
        //创建表单(头部)
        form1 = $("#form1").ligerForm({
        inputWidth: 170, labelWidth: 90, space: 40,
        validate:true,
        fields: [
				{ display: "单据编号", name: "number", type: "text",newline: false, validate: { required: true, minlength: 5 } },
	            { display: "部门", name: "depGroup.uuid", newline: false, type: "popup",textField:'depGroup.name',
	            	 editor: {
	                    grid: getDepGroupGrid(true),
	                    valueField:'uuid',
	                    textField: 'name',
	                    parms :{date:new Date().getTime()},
	                    width: 300,
	                    height:100,
	                    realTimeLoad:true
	           	 }
	           },
				{ display: "拆包日期", name: "bizDate", type: "date",newline: false, validate: { required: true, minlength: 5 } },
				{ display: "仓库", name: "warehouse", newline: false, type: "text"},
				{ display: "业务类型", name: "bizType", newline: false,  type: "text" },
				{ display: "备注", name: "remark", newline: true, type: "text", width: 470}
            ]
        });
        
        //创建分录
        grid = $("#grid").ligerGrid({
            columns: [
					  { display: '商品编码', name: 'material.uuid', type: 'popup', width: 100,textField:'material.number',
						  	editor: {type: 'popup',
						     grid: getMaterialGrid(true),
						     valueField: 'uuid',
						     textField: 'number',
						     parms :{date:new Date().getTime()},
						     width: 300,
						     height:100,
						     realTimeLoad:true,
						     onSelected:function(e){
						    	var ec =  grid.curEditColumn;
						    	grid.updateCell('materialName', e.data[0].name, ec.rowData);
						    	grid.updateCell('materialStandard', e.data[0].standard, ec.rowData);
						    	grid.updateCell('jilUnit', e.data[0].baseUnit, ec.rowData);
						   	 // console.log(e);
						     }
						  }
					   },
					  { display: '商品名称', name: 'materialName', width: 100, editor: { type: 'text' }},
					  { display: '商品规格型号', name: 'materialStandard', width: 100, editor: { type: 'text' }},
					  { display: '批次', name: 'lot', width: 100, editor: { type: 'text' }},
					  { display: '条码', name: 'barCode', width: 100, editor: { type: 'text' }},
					  { display: '保质期', name: 'expiraDate', width: 100, editor: { type: 'date' }},
					  { display: '库存计量单位', name: 'InventoryJilUnit', width: 100, editor: { type: 'text' }},
					  { display: '库存数量', name: 'InventoryQty', width: 100, editor: { type: 'number' },totalSummary:{type: 'sum'}},
					  { display: '转换计量单位', name: 'switchJilUnit', width: 100, editor: { type: 'text' }},
					  { display: '换算率', name: 'matrixingRate', width: 100, editor: { type: 'float' }},
					  { display: '拆包后数量', name: 'umpackBackQty', width: 100, editor: { type: 'number' },totalSummary:{type: 'sum'}},
					  { display: '备注', name: 'remark', width: 100, editor: { type: 'text' }},
		    	      { display: 'uuid',name: 'uuid',hide:true, width: 100, type: 'text' }

            ], usePager: false,width: '95%',height: '275px',enabledEdit: true,alternatingRow:true,
            checkbox: true,headerRowHeight:22, rowHeight:22, rownumbers:true,
            onAfterEdit: f_onAfterEdit,

            toolbar: { items: [
                               { text: '添加行', click: addNewRow,icon: 'add' },
                               { line: true },
                               { text: '删除行', click: delRow,icon: 'delete' }
                               ]
                     }
            
        });
        //创建表单(尾部)
        form2 = $("#form2").ligerForm({
        inputWidth: 170, labelWidth: 90, space: 40,
        validate:true,
        fields: [
            
            { display: "创建人", name: "createUser.uuid", type: 'popup',newline: false,textField: 'createUser.trueName',
            	 editor: {
                     grid: getSysUserGrid(true),
                     valueField:'uuid',
                     textField: 'trueName',
                     parms :{date:new Date().getTime()},
                     width: 300,
                     height:100,
                     realTimeLoad:true
            	 }
            },
            { display: "修改人", name: "alterUser.uuid", type: 'popup',newline: false,textField:'alterUser.trueName',
           	 editor: {
                    grid: getSysUserGrid(true),
                    valueField:'uuid',
                    textField: 'trueName',
                    parms :{date:new Date().getTime()},
                    width: 300,
                    height:100,
                    realTimeLoad:true
           	 }
           },
            { display: "审核人", name: "auditUser.uuid", type: 'popup',newline: false,textField:'auditUser.trueName',
              	 editor: {
                       grid: getSysUserGrid(true),
                       valueField:'uuid',
                       textField: 'trueName',
                       parms :{date:new Date().getTime()},
                       width: 300,
                       height:100,
                       realTimeLoad:true
              	 }
              },
            
            { display: "创建日期", name: "createDate", newline: false, type: "date"},
            { display: "修改日期", name: "alterDate", newline: false,  type: "date" },
            { display: "审核日期", name: "auditDate",newline: false, type: "date" },
            { display: "提交人", name: "submitUser.uuid", type: 'popup',newline: false,textField:'submitUser.trueName',
             	 editor: {
                      grid: getSysUserGrid(true),
                      valueField:'uuid',
                      textField: 'trueName',
                      parms :{date:new Date().getTime()},
                      width: 300,
                      height:100,
                      realTimeLoad:true/* ,
                      onSelected:function(e){
                    	  var edd = form2.getEditor("createUser.uuid"); //form中应用
                    	  edd.setText(e.text);
                    	  console.log(e);
                      } */
             	 }
             },
            { display: "提交日期", name: "submitDate",newline: false, type: "date" },
            { name: "uuid", type: "hidden" },
            { display: "单据状态", name: "status", newline: false, type: "select",editor: {data:getStatsSelect()} }
            ]
        });
      //设置不可编辑字段
    	setBillEnable();
      	var info = '${info}';
    	if( info != null && info != ""){
    		var infoData = JSON.parse(info);
    		console.log(infoData);
    		form2.setData(infoData);
    		form1.setData(infoData);
    		grid.set({ data: {Rows:infoData.entrys}}); 
    	}
    	//单据状态
   		setEditStatus('${editStatus}',form1,form2,grid);
	});
	
	 //编辑后事件 
	function f_onAfterEdit(e){
		/* if(e.column.name == "material.uuid"){
			alert(e.value);
			grid.updateCell('materialName', '3333', e.record);
		} */
	}
	
	
</script>
</head>
<body>
  <div id="toptoolbar"></div>
  <form id="form1"></form>
  <div id="grid" style="margin-top:15x"></div>
  <form id="form2" style="margin-top:15px"></form>
</body>
<script type="text/javascript">
//添加分录
function addNewRow(){
	grid.addRow();
}
//删除分录
function delRow(){
	grid.deleteSelectedRow();
}
//保存
function saveForm(){
	var delData = grid.getDeleted();
	var gridData = grid.getData();
	var form1Data = form1.getData();
	var form2Data = form2.getData();
	if(validationSave($.ligerDialog,form2Data.status)){
		return;
	};
	var formData = $.extend({}, form1Data,form2Data);
	formData.entrys = gridData;
	$.ajax({
        loading: '正在保存数据中...',
        type:'post',
        url: "/inventoryUnpack/saveOrUpdateInventoryUnpack",
		dataType:'json',
        data: {formData:JSON.stringify(formData),delData:JSON.stringify(delData),gridData:JSON.stringify(gridData)},
        success: function (data) {
				$.ligerDialog.success(data.message,'信息',function(){
					form2.setData(data.data);
			    });
        },
        error: function (message) {
       	 	$.ligerDialog.error("保存失败");
        }
    });
}
//提交
function submitForm(){
	var info = form2.getData();
	if(validationSubmit($.ligerDialog,info.status)){
		return;
	};
	$.ajax({
        loading: '正在保存数据中...',
        type:'post',
        url: "/inventoryUnpack/submitInventoryUnpack",
		dataType:'json',
        data: {uuid:info.uuid},
        success: function (data) {
				$.ligerDialog.success(data.message,'信息',function(){
		    		form2.setData(data.data);
			    });
        },
        error: function (message) {
       	 	$.ligerDialog.error("提交失败");
        }
    });
}
//审核
function auditForm(){
	var info = form2.getData();
	if(validationAudit($.ligerDialog,info.status)){
		return;
	};
	$.ajax({
        loading: '正在审核数据中...',
        type:'post',
        url: "/inventoryUnpack/auditInventoryUnpack",
		dataType:'json',
        data: {uuid:info.uuid},
        success: function (data) {
				$.ligerDialog.success(data.message,'信息',function(){
		    		form2.setData(data.data);
			    });
        },
        error: function (message) {
       	 	$.ligerDialog.error("审核失败");
        }
    });
}
//反审核
function unAuditForm(){
	var info = form2.getData();
	if(validationUnAudit($.ligerDialog,info.status)){
		return;
	};
	$.ajax({
        loading: '正在反审核数据中...',
        type:'post',
        url: "/inventoryUnpack/unAuditInventoryUnpack",
		dataType:'json',
        data: {uuid:info.uuid},
        success: function (data) {
				$.ligerDialog.success(data.message,'信息',function(){
		    		form2.setData(data.data);
			    });
        },
        error: function (message) {
       	 	$.ligerDialog.error("反审核失败");
        }
    });
}
//不可编辑字段
function setBillEnable(){
	//form2.setEnabled(["createUser","createDate","submitUser","submitDate","auditUser","auditDate","alterUser","alterDate","status"],false); 
}
</script>
</html>